#include <string.h>
#include <stdio.h>
#include <stdlib.h>

/*
 * C标准
 *
 * 将字符串分割成一个个片段
 *
 *	strtok内部记录上次调用字符串的位置，所以是不可重入的，不支持多线的，其可重入版本为strtok_r
 */

int strtok1()
{
	char * buf2 = "aaa, ,a, ,,,bbb-c,,,ee|abc";
	char buf1[] = {"aaa, ,a, ,,,bbb-c,,,ee|abc"};
	/* Establish string and get the first token: */
	char * token = strtok( buf1, ",-|");

	while( token != NULL )
	{
		/* While there are tokens in "string" */
		printf( "%s ", token );						/* aaa   a   bbb c ee abc */
		/* Get next token: */
		token = strtok( NULL, ",-|");
	}
	printf( "\n");

	return 0;
}

int strtok2()
{
	char *s = "Golden Global View";
	char ss[] = {"Golden:Global:View"};
	char *d = ":";
	char *p;

	p=strtok(ss,d);
	while(p != NULL)
	{
		printf("%s\n",p);
		p = strtok('\0',d);
	}

	/*
	  Golden
	  Global
	  View
	 */
}

int strtok4()
{
	int in = 0, j;
	char buffer[] = "Fred,John,Ann";
	char *p[3];							// note
	char *buff = buffer;
	char result[3][8];

	while((p[in] = strtok(buff,",")) != NULL) {
		printf("%s\n", p[in]);
		in++;
		buff = NULL; 
	}

	printf("p = %s %s %s\n", p[0],p[1],p[2]);	/* Fred John Ann */
}	

int strtok5()
{
	int in = 0;
	char buffer[255] = "Fred male 25,John male 62,Anna female 16";
	char *p[20];
	char *buf = buffer;
	int j;

	while((p[in] = strtok(buf,",")) != NULL) {
		buf = p[in];
		while((p[in] = strtok(buf," ")) != NULL) {
			in++;
			buf = NULL;
		}
		p[in++] = "***"; //±íÏÖ·Ö¸î
		buf = NULL; 
	}

	printf("Here we have %d strings\n",in);
	for (j=0; j<in; j++)
		printf(">%s<\n",p[j]);

	return 0;
}

int strtok6()
{
	int in = 0;
	int j;
	char buffer[255] = "Fred male 25,John male 62,Anna female 16";
	char *p[20];
	char *buf = buffer;

	char *outer_ptr = NULL;
	char *inner_ptr = NULL;

	while((p[in] = strtok_r(buf,",",&outer_ptr)) != NULL) {
		buf = p[in];
		while((p[in] = strtok_r(buf," ",&inner_ptr)) != NULL) {
			in++;
			buf = NULL;
		}
		p[in++] = "***";
		buf = NULL; 
	}

	printf("Here we have %d strings\n",in);
	for (j=0; j<in; j++)
		printf(">%s<\n",p[j]);

	return 0;
}

int time_analysis(void)
{
	char * p;
	char buf[256];
	const char * delim = ";";
	char string[] = "#MARKTIMEA,COM1,0,41.0,FINESTEERING,1975,457422.959,00000000,292e,14392;1975,457422.958801440,7.184707455e-08,1.391773761e-09,-18.000000000,VALID*4f9858ff";

	memset(buf, 0, sizeof buf);
	p = strtok(string, delim);
	if (p)   
		printf("%s\n", p);
	
	p = strtok(NULL, delim); 
	if (p)   
		printf("%s\n", p); 

	strcpy(buf, p);

	printf("b:%s\n", buf);

	return 0;
}

int time_analysis_(void)
{
	char * p;
	char buf[256];
	const char * delim = "[]";
	char string[] = "[#MARKTIMEA,COM1,0,41.0,FINESTEERING,1975,457422.959,00000000,292e],14392;1975,457422.958801440,7.184707455e-08,[1.391773761e-09,-18.000000000,VALID*4f9858ff]";

	p = strtok(string, delim);

	while (p != NULL) {
		printf("%s\n", p);
		memset(buf, 0, sizeof buf);
		strcpy(buf, p);
		p = strtok(NULL, delim);
	}

	return 0;
}

void gga()
{
	char * pstr = NULL;
	char gga_rmc[] = {"\r\n$GNRMC,082935.000,A,2307.335517,N,11320.737380,E,0.000,0.00,010221,,E,A*09\r\n$GNGGA,082935.000,2307.335517,N,11320.737380,E,1,15,0.83,66.504,M,0,M,,*6E\r\n"};
	char *needle = "\r\n";
	char *result[8];
	int i = 0;

	pstr = gga_rmc;

	while((result[i] = strtok(pstr, needle)) != NULL) {
		printf("result[%d] = %s len = %ld\n", i, result[i], strlen(result[i]));
		i++;
		pstr = NULL; 
	}
}

int main()
{
	//strtok1();
	//strtok2();
	//strtok4();
	//strtok5();
	strtok6();

	//time_analysis();
	time_analysis_();
	gga();

	return 0;
}
